package cn.melonlib;

import cn.melonlib.cores.commons.comp.utils.ApplicationContextUtils;
import cn.melonlib.cores.commons.comp.utils.YamlSourceFactory;
import cn.melonlib.cores.web.comp.interceptor.WebInfoInterceptor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@SpringBootApplication
@PropertySource(value = {"classpath:db.yml","classpath:fs.yml","classpath:register.yml"}, factory = YamlSourceFactory.class)
@ImportResource({"classpath:spring-context-datasource.xml"})
@EnableCaching
@EnableTransactionManagement(order = 5)
public class MainBootApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        ApplicationContextUtils.setApplicationContext(SpringApplication.run(MainBootApplication.class, args)) ;
    }


    @Resource
    private WebInfoInterceptor webInfoInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTION");
    }

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(webInfoInterceptor).addPathPatterns("/**");
//        registry.addInterceptor(new BasicInterceptor()).addPathPatterns("/provider");
    }


    //    @Override
//    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
//        return builder.sources(TeachApplication.class);
//    }
}
